package com.yfjin.ffmpeg54.player;

import android.text.TextUtils;

/**
 * @author jinyf
 */
public class MNPlayer {

    private native void native_prepare(String source);
    private native void native_start();
    private native void native_seek(int second);
    private native void native_pause();
    private native void native_resume();
    private native void native_speed(float speed);

    private native void native_mute(int i);
    private native void native_pitch(float i);

    private native void native_stop();

    private String source;
    private MNOnPrepareListener mnOnPrepareListener;
    private IPlayerListener mIPlayerListener;


    public void setSource(String source) {
        this.source = source;
    }

    public void setMnOnPrepareListener(MNOnPrepareListener mnOnPrepareListener) {
        this.mnOnPrepareListener = mnOnPrepareListener;
    }

    public void setPlayerListener(IPlayerListener playerListener) {
        this.mIPlayerListener = playerListener;
    }


    public void prepared() {
        if (TextUtils.isEmpty(source)) {
            return;
        }
        native_prepare(source);
    }

    public void start() {
        if (TextUtils.isEmpty(source)) {
            return;
        }
        new Thread(() -> native_start()).start();

    }


    public void seekTo(int second) {
        if (TextUtils.isEmpty(source)) {
            return;
        }
        new Thread(() -> native_seek(second)).start();
    }



    //c++回调java方法
    public void onCallPrepared() {
        mnOnPrepareListener.onPrepared();
    }


    public void onCallTimeInfo(int currentTime, int totalTime) {
        if (mIPlayerListener == null) {
            return;
        }
        mIPlayerListener.onCurrentTime(currentTime, totalTime);
    }


    private boolean isPaused=false;

    public boolean pauseOrResume() {
        if (isPaused){
            native_resume();
            isPaused=false;
        }else{
            native_pause();
            isPaused=true;
        }
        return isPaused;
    }


    public void setMute(int i){
        native_mute(i);
    }

    public void setSpeed(float speed){
        native_speed(speed);
    }


    public void setPicth(float pitch) {
        native_pitch(pitch);
    }


    public void stop() {
        native_stop();
    }
}
